from typing import Tuple

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm.session import Session

import settings
from db.models import Base


def get_engine_and_session_of_db() -> Tuple[Engine, Session]:
    """ Получаем объекты session и engine для работы с базой данных """

    record_connect_to_db = '{}+{}://{}:{}@{}/{}'.format(
         settings.DIALECT_DB,
         settings.DRIVER_DB,
         settings.POSTGRES_USER,
         settings.POSTGRES_PASSWORD,
         settings.POSTGRES_HOST,
         settings.POSTGRES_NAME
    )
    engine = create_engine(record_connect_to_db)
    Session = sessionmaker(bind=engine)
    session = Session()
    return (engine, session)


def close_engine_and_session_of_db(engine: Engine, session: Session) -> None:
    """ Завершаем работу с базой данных """

    session.close()
    engine.dispose()


def create_necessary_data_in_db(engine: Engine) -> None:
    """ Создаем необходимые данные в бд """

    Base.metadata.create_all(engine)
